package com.lm.jsqlparser.example;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectExpressionItem;

public class AddNewField {

    public static void main(String[] args) throws JSQLParserException {
        String sql = "SELECT id, name FROM users";

        // 解析 SQL
        Select selectStatement = (Select) CCJSqlParserUtil.parse(sql);
        PlainSelect plainSelect = (PlainSelect) selectStatement.getSelectBody();

        // 添加新的字段
        SelectExpressionItem newField = new SelectExpressionItem();
        newField.setExpression(CCJSqlParserUtil.parseExpression("email"));
        plainSelect.getSelectItems().add(newField);

        // 输出修改后的 SQL
        System.out.println("修改后的 SQL：" + selectStatement);
    }
}
